﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.ComponentModel.DataAnnotations;
using System.Web.Hosting;
using System.IO;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations.Schema;

namespace DYSales.Web
{
    public class EntityContext : DbContext
    {
        public DbSet<Operator> Operators { get; set; }
        public DbSet<QcTable> QcTables { get; set; }
        public DbSet<PdTable> PdTables { get; set; }
        public DbSet<SellTable> SellTables { get; set; }
        public DbSet<SellDetail> SellDetails { get; set; }
        public DbSet<StockAdd> StockAdds { get; set; }
        public DbSet<StockDel> StockDels { get; set; }
        public DbSet<ExpenditureTable> ExpenditureTables { get; set; }
        public DbSet<PdInventory> PdInventorys { get; set; }
        public DbSet<Inventory> Inventorys { get; set; }
        public DbSet<Set_table> Set_tables { get; set; }

        public EntityContext()
        {
            //using sql compact 4.0
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "", conCE.constring);
            string folder = HostingEnvironment.MapPath(@"~\App_Data");
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            if (!File.Exists(HostingEnvironment.MapPath(@"~\App_Data\data.sdf")))
            {
                Database.SetInitializer<EntityContext>(new SystemInitializer());
            }

            ////using sql server express 2008 r2
            //Database.DefaultConnectionFactory = new SqlConnectionFactory(conSQL.GetConString());
            //if (!Database.Exists(conSQL.GetConString()))
            //{
            //    Database.SetInitializer<EntityContext>(new SystemInitializer());
            //}
        }

        public override int SaveChanges()
        {
            bool saveFailed;
            int rt = 0;
            do
            {
                saveFailed = false;
                try
                {
                    rt = base.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    saveFailed = true;
                    var entry = ex.Entries.Single();
                    if (entry.State == System.Data.EntityState.Deleted)
                    {
                        entry.State = System.Data.EntityState.Detached;
                    }
                }
            } while (saveFailed);
            return rt;
        }
    }

    public class Set_table
    {
        [Key]
        public long id { get; set; }
        [Required(ErrorMessage = "店名不能空")]
        public string s_店名 { get; set; }
        public string s_副标题 { get; set; }
        public string s_电话 { get; set; }
        public string s_地址 { get; set; }
        public double s_打印页宽 { get; set; }
        public double s_字体大小 { get; set; }
        public DateTime s_营业开始时间 { get; set; }
        public DateTime s_营业结束时间 { get; set; }
    }

    public class Inventory
    {
        [Key]
        [Display(Name = "ID", Order = 0)]
        public long id { get; set; }
        [Display(Name = "商品编号", Order = 1)]
        [Required(ErrorMessage = "商品编号不能空")]
        public string p_商品编号 { get; set; }
        [Display(Name = "单价", Order = 2)]
        [Required(ErrorMessage = "单价不能空")]
        public decimal p_单价 { get; set; }
        [Display(Name = "商品名称", Order = 3)]
        [Required(ErrorMessage = "商品名称不能空")]
        public string p_商品名称 { get; set; }
        [Display(Name = "拼音简码", Order = 4)]
        public string p_拼音简码 { get; set; }
        [Display(Name = "盘点数量", Order = 5)]
        public decimal p_盘点数量 { get; set; }
        [Display(Name = "库存数量", Order = 6)]
        public decimal p_库存数量 { get; set; }
        [Display(Name = "盘点结果", Order = 7)]
        public decimal p_盘点结果 { get; set; }
        [Display(Name = "条形码", Order = 8)]
        public string p_条形码 { get; set; }
        [Display(Name = "操作员", Order = 9)]
        public string p_操作员 { get; set; }
        [Display(Name = "盘点日期", Order = 10)]
        public DateTime p_盘点日期 { get; set; }
    }

    public class PdInventory
    {
        [Key]
        [Display(Name = "商品编号", Order = 1)]
        [Required(ErrorMessage = "商品编号不能空")]
        public string p_商品编号 { get; set; }
        [Display(Name = "单价", Order = 2)]
        [Required(ErrorMessage = "单价不能空")]
        public decimal p_单价 { get; set; }
        [Display(Name = "商品名称", Order = 3)]
        [Required(ErrorMessage = "商品名称不能空")]
        public string p_商品名称 { get; set; }
        [Display(Name = "拼音简码", Order = 4)]
        public string p_拼音简码 { get; set; }
        [Display(Name = "盘点数量", Order = 5)]
        public decimal p_盘点数量 { get; set; }
        [Display(Name = "库存数量", Order = 6)]
        public decimal p_库存数量 { get; set; }
        [Display(Name = "盘点结果", Order = 7)]
        public decimal p_盘点结果 { get; set; }
        [Display(Name = "条形码", Order = 8)]
        public string p_条形码 { get; set; }
        [Display(Name = "操作员", Order = 9)]
        public string p_操作员 { get; set; }
    }

    public class ExpenditureTable
    {
        [Key]
        [Display(Name = "ID", Order = 1)]
        public long id { get; set; }
        [Required(ErrorMessage = "支出类别不能空")]
        [Display(Name = "支出类别 ", Order = 2)]
        public string exp_类别 { get; set; }
        [Display(Name = "支出金额 ", Order = 3)]
        public decimal exp_支出金额 { get; set; }
        [Display(Name = "备注 ", Order = 4)]
        public string exp_备注 { get; set; }
        [Display(Name = "时间 ", Order = 5)]
        public DateTime exp_时间 { get; set; }
    }

    public class StockAdd
    {
        [Key]
        [Display(Name = "ID", Order = 1)]
        public long id { get; set; }
        [Display(Name = "批号", Order = 2)]
        public string st_批号 { get; set; }
        [Required(ErrorMessage = "商品编号不能空")]
        [Display(Name = "商品编号", Order = 3)]
        public string st_商品编号 { get; set; }
        [Required(ErrorMessage = "商品名称不能空")]
        [Display(Name = "商品名称 ", Order = 4)]
        public string st_商品名称 { get; set; }
        [Display(Name = "进货时间", Order = 5)]
        public DateTime st_进货时间 { get; set; }
        [Display(Name = "进货数量", Order = 6)]
        public decimal st_进货数量 { get; set; }
        [Display(Name = "进货价", Order = 7)]
        public decimal st_进货价 { get; set; }
        [Display(Name = "操作员", Order = 8)]
        public string st_操作员 { get; set; }
    }

    public class StockDel
    {
        [Key]
        [Display(Name = "ID", Order = 1)]
        public long id { get; set; }
        [Required(ErrorMessage = "商品编号不能空")]
        [Display(Name = "商品编号", Order = 2)]
        public string st_商品编号 { get; set; }
        [Required(ErrorMessage = "商品名称不能空")]
        [Display(Name = "商品名称 ", Order = 3)]
        public string st_商品名称 { get; set; }
        [Display(Name = "退货时间", Order = 4)]
        public DateTime st_退货时间 { get; set; }
        [Display(Name = "退货数量", Order = 5)]
        public decimal st_退货数量 { get; set; }
        [Display(Name = "进货价", Order = 6)]
        public decimal st_进货价 { get; set; }
        [Display(Name = "操作员", Order = 7)]
        public string st_操作员 { get; set; }
    }

    public class SellTable
    {
        public SellTable()
        {
            SellDetails = new List<SellDetail>();
        }
        [Key]
        [Display(Name = "ID", Order = 1)]
        public long id { get; set; }
        [Display(Name = "消费时间", Order = 2)]
        public DateTime st_消费时间 { get; set; }
        [Display(Name = "消费金额", Order = 3)]
        public decimal st_消费金额 { get; set; }
        [Display(Name = "现金支付", Order = 4)]
        public decimal st_现金支付 { get; set; }
        [Display(Name = "刷卡支付", Order = 5)]
        public decimal st_刷卡支付 { get; set; }
        [Display(Name = "实付金额", Order = 6)]
        public decimal st_实付金额 { get; set; }
        [Display(Name = "整单折扣", Order = 7)]
        public double st_整单折扣 { get; set; }
        [Display(Name = "操作员", Order = 8)]
        public string st_操作员 { get; set; }
        [Display(Name = "单号", Order = 9)]
        public string st_流水号 { get; set; }

        [Include]
        [Display(Name = "外键", Order = 10)]
        public virtual ICollection<SellDetail> SellDetails { get; set; }
    }

    public class SellDetail
    {
        [Key]
        [Display(Name = "ID", Order = 1)]
        public long id { get; set; }
        [Display(Name = "商品编号", Order = 2)]
        public string sd_商品编号 { get; set; }
        [Display(Name = "商品名称", Order = 3)]
        public string sd_商品名称 { get; set; }
        [Display(Name = "单价", Order = 4)]
        public decimal sd_单价 { get; set; }
        [Display(Name = "数量", Order = 5)]
        public decimal sd_数量 { get; set; }
        [Display(Name = "进货价", Order = 6)]
        public decimal sd_进货价 { get; set; }
        [Display(Name = "单位", Order = 7)]
        public string sd_单位 { get; set; }
        [Display(Name = "小计", Order = 8)]
        public decimal sd_小计 { get; set; }
        [Display(Name = "折扣", Order = 9)]
        public double sd_折扣 { get; set; }

        [Display(Name = "外键", Order = 10)]
        public long sid { get; set; }
        [Display(Name = "外键", Order = 11)]
        [ForeignKey("sid")]
        public virtual SellTable SellTable { get; set; }
    }

    public class PdTable
    {
        [Key]
        [Display(Name = "ID", Order = 0)]
        public long id { get; set; }
        [Required(ErrorMessage = "商品编号不能空")]
        [Display(Name = "商品编号", Order = 1)]
        public string p_商品编号 { get; set; }
        [Required(ErrorMessage = "商品名称不能空")]
        [Display(Name = "商品名称", Order = 2)]
        public string p_商品名称 { get; set; }
        [Required(ErrorMessage = "单价不能空")]
        [Display(Name = "单价", Order = 3)]
        public decimal p_单价 { get; set; }
        [Display(Name = "拼音简码", Order = 4)]
        public string p_拼音简码 { get; set; }
        [Display(Name = "库存数量", Order = 5)]
        public decimal p_库存数量 { get; set; }
        [Display(Name = "条形码", Order = 6)]
        public string p_条形码 { get; set; }
        [Display(Name = "进货价", Order = 7)]
        public decimal p_进货价 { get; set; }
        [Display(Name = "单位", Order = 8)]
        public string p_单位 { get; set; }
        [Display(Name = "库存下限", Order = 9)]
        public decimal p_库存下限 { get; set; }
        [Required(ErrorMessage = "商品类别不能空")]
        [Display(Name = "商品类别", Order = 10)]
        public string p_商品类别 { get; set; }
        [Range(0.01, 1, ErrorMessage = "折扣值只能在0.01到1之间")]
        [Display(Name = "折扣", Order = 11)]
        public double p_折扣 { get; set; }
    }

    public class Operator
    {
        public Operator()
        {
            QcTables = new List<QcTable>();
        }
        [Key]
        [Display(Name = "ID", Order = 1)]
        public int id { get; set; }
        [Required(ErrorMessage = "用户名不能空")]
        [Display(Name = "用户名", Order = 2)]
        public string ot_用户名 { get; set; }
        [Required(ErrorMessage = "密码不能空")]
        [Display(Name = "密码", Order = 3)]
        public string ot_密码 { get; set; }
        [Display(Name = "权限组", Order = 4)]
        public string ot_权限组 { get; set; }
        [Required(ErrorMessage = "姓名不能空")]
        [Display(Name = "姓名", Order = 5)]
        public string ot_姓名 { get; set; }
        [Display(Name = "启用", Order = 6)]
        public bool ot_启用 { get; set; }

        [Include]
        public virtual ICollection<QcTable> QcTables { get; set; }
    }

    public class QcTable
    {
        [Key]
        public int id { get; set; }
        public string qt_qc { get; set; }

        public int oid { get; set; }
        [ForeignKey("oid")]
        public virtual Operator Operator { get; set; }
    }

}